package com.admission.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.communicate.KeyTools;
import com.admission.communicate.OrderBuilder;
import com.admission.model.Order;
import com.admission.model.OrderTemp;
import com.admission.model.PayLog;
import com.admission.service.OrderTempManager;
import com.admission.service.PayLogManager;
import com.admission.util.MoneyUtils;
import com.admission.util.PageUtils;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class OrderTempAction extends CrudActionSupport<OrderTemp>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6075483082973208029L;
	private Order order;
	private Page<OrderTemp> page = new Page<OrderTemp>(PageUtils.DEFAULT_PAGE_SIZE);
	private String id;
	private PayLogManager payLogManager;
	private OrderTempManager orderTempManager;
	private PayLog payLog;
	private String userIdentify;
	private OrderTemp orderTemp;
	private String RMB;
	HttpServletRequest request=ServletActionContext.getRequest();
	
	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}
	public Page<OrderTemp> getPage() {
		return page;
	}

	public void setPage(Page<OrderTemp> page) {
		this.page = page;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public OrderTemp getOrderTemp() {
		return orderTemp;
	}

	public void setOrderTemp(OrderTemp orderTemp) {
		this.orderTemp = orderTemp;
	}

	public PayLogManager getPayLogManager() {
		return payLogManager;
	}

	public void setPayLogManager(PayLogManager payLogManager) {
		this.payLogManager = payLogManager;
	}
	
	public OrderTempManager getOrderTempManager() {
		return orderTempManager;
	}

	public void setOrderTempManager(OrderTempManager orderTempManager) {
		this.orderTempManager = orderTempManager;
	}
	public PayLog getPayLog() {
		return payLog;
	}

	public void setPayLog(PayLog payLog) {
		this.payLog = payLog;
	}

	public void setUserIdentify(String userIdentify) {
		this.userIdentify = userIdentify;
	}
	public String getRMB() {
		return RMB;
	}

	public void setRMB(String rMB) {
		RMB = rMB;
	}

	
	@Override
	public String delete() throws Exception {
		try{
			orderTempManager.deleteById(id);
		} catch (Exception e) {
		   this.addActionMessage("删除失败，请联系管理员");
	    }
		
		return this.list();
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
		.buildFromHttpRequest(Struts2Utils.getRequest());
		String mobile = request.getSession().getAttribute("USER_PHONE").toString();
		if (!StringUtils.isEmpty(mobile)) {
			PropertyFilter p1 = new PropertyFilter("EQS_mobile",
					mobile);
			filters.add(p1);
		}
		page=orderTempManager.getPageByFilter(page, filters);
		List<OrderTemp> orderTempList = page.getResult();
		if(orderTempList.size() > 0){
			String orderID = orderTempList.get(0).getOrderId();
			order = OrderBuilder.CreatOrder();		
			order.setOrdId(orderID); 
			int totalAmount = Integer.parseInt(order.getOrdAmt()) * (int)page.getTotalCount();
			order.setOrdAmt(String.valueOf(totalAmount));
			order.setShareData(order.getShareAccount() + "^" + order.getOrdAmt());
			String signString = KeyTools.GenSignString(orderID, String.valueOf(totalAmount));
			order.setChkValue(signString);
			SavePlainData(order);//保存订单明文，返回支付结果时做验证
			//显示订单总额
			setRMB(MoneyUtils.stringToMoney(order.getOrdAmt()));
			
			return "order-temp";
		}
		else {
			return "order-temp";
		}
		
	}

	private void SavePlainData(Order order) {
		String plainData = order.getMerId() + order.getOrdId() + order.getOrdAmt() + order.getCuryId() + order.getVersion()
		+ order.getBgRetUrl() + order.getPageRetUrl() + order.getShareType() + order.getShareData();
		
		HttpSession session=request.getSession(false);
	      session.setAttribute("PLAINDATA", plainData);
	      
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			orderTemp = new OrderTemp();
		} else {
			orderTemp = orderTempManager.getOrderTempById(id);
		}
	}

	@Override
	public String save() throws Exception {
		try{
			payLog = payLogManager.getPayLogByIdNum(userIdentify);
		    orderTemp.setMobile(request.getSession().getAttribute("USER_PHONE").toString());
		    orderTemp.setExamineeNo(payLog.getExamieeNo());
		    orderTemp.setIdnum(payLog.getIdnum());
		    orderTemp.setName(payLog.getName());
		    orderTemp.setAmount(payLog.getAmount());
		    orderTempManager.save(orderTemp);
		    this.addActionMessage("添加成功");
			
		} catch (Exception e) {
		   this.addActionMessage("添加失败，请联系管理员");
	    }
		return this.list();
	}

	@Override
	public OrderTemp getModel() {
		// TODO Auto-generated method stub
		return null;
	}

}
